<?php

namespace Trawind\Basics\Http;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Trawind\Basics\Enums\SystemConstant;
use Trawind\Helpers\ResponseHelper;
use Trawind\Basics\Traits\ResponseFormat;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Trawind\Basics\Requests\BatchOperationRequest;
use Prettus\Repository\Contracts\RepositoryInterface;
use Illuminate\Routing\Controller as LaravelController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class BaseController extends LaravelController
{

    use  AuthorizesRequests, DispatchesJobs, ValidatesRequests, ResponseFormat;


    /**
     * @var RepositoryInterface
     */
    protected $repository;


    protected $needCheckAuth = true;


    protected $authIgnoreAction = [];

    /**
     * Controller constructor.
     */
    public function __construct()
    {
        if ($this->needCheckAuth) {
            $data = '';
            if ($this->authIgnoreAction) {
                $data = ':'.base64_encode(json_encode([static::class => $this->authIgnoreAction], true));
            }
            $this->middleware(\Trawind\VerifyPermission\Middleware\CheckOperactionPermission::class.$data);
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function index()
    {
        return ResponseHelper::ok($this->repository->view());
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return JsonResponse
     */
    public function show($id)
    {
        return ResponseHelper::ok($this->repository->skipFind($id));
    }


    /**
     * batch Remove the specified resource from storage.
     *
     * @return JsonResponse
     * @throws \Throwable
     */
    public function batchDestroy(BatchOperationRequest $request)
    {
        $data = DB::transaction(function () use ($request) {
            return $this->repository->batchDelete($request->input('ids'));
        });

        return ResponseHelper::delete($data);
    }


    /**
     * @param  BatchOperationRequest  $request
     * @return JsonResponse
     * @throws \Throwable
     */
    public function batchEnable(BatchOperationRequest $request)
    {
        $data = DB::transaction(function () use ($request) {
            return $this->repository->batchUpdateStatus($request->input('ids'), SystemConstant::STATUS_ENABLE);
        });

        return ResponseHelper::update($data);
    }

    /**
     * @param  BatchOperationRequest  $request
     * @return JsonResponse
     * @throws \Throwable
     */
    public function batchDisable(BatchOperationRequest $request)
    {
        $data = DB::transaction(function () use ($request) {
            return $this->repository->batchUpdateStatus($request->input('ids'), SystemConstant::STATUS_DISABLE);
        });

        return ResponseHelper::update($data);
    }

    /**
     * @param  BatchOperationRequest  $request
     * @return JsonResponse
     * @throws \Throwable
     */
    public function batchInvalid(BatchOperationRequest $request)
    {
        $data = DB::transaction(function () use ($request) {
            return $this->repository->batchUpdateStatus($request->input('ids'), SystemConstant::STATUS_INVALID);
        });

        return ResponseHelper::update($data);
    }

}
